package com.lemon.smartsafetynew.ui.widget;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrew on 2017/8/9.
 */

public class MonitorSortView extends SurfaceView implements SurfaceHolder.Callback2 {

    private SurfaceHolder surfaceHolder;
    private ViewThread mThread;

    private Paint imagePaint;
    private Paint textPaint;

    private List<CircleDataVo> dataList = new ArrayList<>();
    private Float[] sizeArray = new Float[]{
            0.25f, 0.23f, 0.21f, 0.19f, 0.17f
    };

    private Float[] offsetStatus = new Float[]{
            1f, -1f, -1f, -1f, -1f
    };

    private static final float SHAKE_OFFSET = 5f;
    private static final float BOUNDARY_OFFSET = 20f;


    private float coefficient;

    private volatile boolean flag = false;

    public MonitorSortView(Context context) {
        super(context);
        initView();
    }

    public MonitorSortView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MonitorSortView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setDataList(List<CircleDataVo> dataList) {
        this.dataList = dataList;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    private void initView() {
        imagePaint = new Paint();
        textPaint = new Paint();
        imagePaint.setColor(Color.parseColor("#F6B54E"));
        imagePaint.setAntiAlias(true);
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(CommonUtil.convertDipOrPx(IApplication.context(), 10));
        surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        //mThread = new ViewThread(surfaceHolder);
    }

    @Override
    public void surfaceRedrawNeeded(SurfaceHolder holder) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        mThread = new ViewThread(surfaceHolder);
        mThread.start();

        final ValueAnimator anim = ValueAnimator.ofFloat(-1f, 1f, -1f);

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                coefficient = (float) animation.getAnimatedValue();
            }
        });
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        anim.setDuration(4000);
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        flag = false;
    }

    public static class CircleDataVo {

        private String value;
        private String times;

        public CircleDataVo(String value, String times) {
            this.value = value;
            this.times = times;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getTimes() {
            return times;
        }

        public void setTimes(String times) {
            this.times = times;
        }
    }

    public class ViewThread extends Thread {
        private SurfaceHolder holder;

        public ViewThread(SurfaceHolder holder) {
            this.holder = holder;
        }

        @Override
        public void run() {
            while (flag) {
                Canvas c = null;
                try {
                    synchronized (holder) {
                        c = holder.lockCanvas();//锁定画布，一般在锁定后就可以通过其返回的画布对象Canvas，在其上面画图等操作了。
                        c.drawColor(Color.WHITE);//设置画布背景颜色
                        if (dataList != null) {
                            for (int i = 0; i < dataList.size(); i++) {

                                float radius = getHeight() * sizeArray[i];
                                //float x = getWidth() / 2;
                                //float y = getHeight() / 2;
                                float x = xPoint(i);
                                float y = yPoint(i);

                                c.drawCircle(x + SHAKE_OFFSET * coefficient * offsetStatus[i], y, radius, imagePaint);
                                Rect targetRect = new Rect((int) (x + 10 * coefficient * offsetStatus[i] - radius),
                                        (int) (y - radius),
                                        (int) (x + SHAKE_OFFSET * coefficient * offsetStatus[i] + radius),
                                        (int) (y + radius));

                                Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
                                int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
                                // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
                                textPaint.setTextAlign(Paint.Align.CENTER);
                                c.drawText(dataList.get(i).getValue() + " " + dataList.get(i).getTimes(), targetRect.centerX(), baseline, textPaint);

                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (c != null) {
                        holder.unlockCanvasAndPost(c);//结束锁定画图，并提交改变。
                    }
                }
            }

        }
    }

    private float xPoint(int position) {
        switch (position) {
            case 0:
                return getWidth() / 2;
            case 1:
                return getHeight() * sizeArray[position] + BOUNDARY_OFFSET;
            case 2:
                return getWidth() - getHeight() * sizeArray[position] - BOUNDARY_OFFSET;
            case 3:
                return getHeight() * sizeArray[position] + BOUNDARY_OFFSET + CommonUtil.convertDipOrPx(IApplication.context(), 50);
            case 4:
                return getWidth() - getHeight() * sizeArray[position] - BOUNDARY_OFFSET - CommonUtil.convertDipOrPx(IApplication.context(), 50);
            default:
                return -100f;
        }
    }

    private float yPoint(int position) {
        switch (position) {
            case 0:
                return getHeight() / 2;
            case 1:
                return getHeight() * sizeArray[position] + BOUNDARY_OFFSET;
            case 2:
                return getHeight() - getHeight() * sizeArray[position] - BOUNDARY_OFFSET;
            case 3:
                return getHeight() - getHeight() * sizeArray[position] - BOUNDARY_OFFSET;
            case 4:
                return getHeight() * sizeArray[position] + BOUNDARY_OFFSET;
            default:
                return -100f;
        }
    }

}

